package com.neu.admin;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.neu.common.RequiresPermissionsDesc;
import com.neu.common.validator.Order;
import com.neu.common.validator.Sort;
import com.neu.doc.AdminAdController_ResOK;
import com.neu.doc.AdminAdController_list;
import com.neu.doc.ResponseOK;
import com.neu.domain.ShoppingAd;
import com.neu.service.ShoppingAdService;
import com.neu.util.ResponseUtil;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/admin/ad")
@Validated
public class AdminAdController {
    private final Log logger = LogFactory.getLog(AdminAdController.class);

    @Autowired
    private ShoppingAdService adService;
    
    @ApiOperation(value="获取广告(List)分页数据", notes="分页查询广告信息",response = AdminAdController_list.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数", required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "每页多少条数据", required = true, dataType = "int"),
            @ApiImplicitParam(name = "sort", value = "排序字段", required = true, dataType = "string"),
            @ApiImplicitParam(name = "order", value = "排序规则", required = true, dataType = "string"),
            @ApiImplicitParam(name = "name", value = "广告名字", required = false, dataType = "string"),
            @ApiImplicitParam(name = "content", value = "广告内容", required = false, dataType = "string")
    })
    @RequiresPermissions("admin:ad:list")
    @RequiresPermissionsDesc(menu = {"推广管理", "广告管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String name, String content,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<ShoppingAd> adList = adService.querySelective(name, content, page, limit, sort, order);
        return ResponseUtil.okList(adList);
    }

    private Object validate(ShoppingAd ad) {
        String name = ad.getName();
        if (StringUtils.isEmpty(name)) {
            return ResponseUtil.badArgument();
        }
        String content = ad.getContent();
        if (StringUtils.isEmpty(content)) {
            return ResponseUtil.badArgument();
        }
        return null;
    }
    

    @ApiOperation(value="添加广告", notes="添加一条广告信息", response = AdminAdController_ResOK.class)
    @RequiresPermissions("admin:ad:create")
    @RequiresPermissionsDesc(menu = {"推广管理", "广告管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@ApiParam(name="ShoppingAd對象",value="传入json格式",required=true)
    		@RequestBody ShoppingAd ad) {
        Object error = validate(ad);
        if (error != null) {
            return error;
        }
        adService.add(ad);
        return ResponseUtil.ok(ad);
    }
    
    @ApiIgnore//使用该注解忽略这个API
    @RequiresPermissions("admin:ad:read")
    @RequiresPermissionsDesc(menu = {"推广管理", "广告管理"}, button = "详情")
    @GetMapping("/read")
    public Object read(@NotNull Integer id) {
        ShoppingAd ad = adService.findById(id);
        return ResponseUtil.ok(ad);
    }
    
    @ApiOperation(value="编辑广告", notes="编辑一条广告信息", response = AdminAdController_ResOK.class)
    @RequiresPermissions("admin:ad:update")
    @RequiresPermissionsDesc(menu = {"推广管理", "广告管理"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@ApiParam(name="ShoppingAd對象",value="传入json格式",required=true)
    		@RequestBody ShoppingAd ad) {
        Object error = validate(ad);
        if (error != null) {
            return error;
        }
        if (adService.updateById(ad) == 0) {
            return ResponseUtil.updatedDataFailed();
        }

        return ResponseUtil.ok(ad);
    }
    

    @ApiOperation(value="删除广告", notes="删除一条广告信息", response = ResponseOK.class)
    @RequiresPermissions("admin:ad:delete")
    @RequiresPermissionsDesc(menu = {"推广管理", "广告管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@ApiParam(name="ShoppingAd對象",value="传入json格式",required=true)
    		@RequestBody ShoppingAd ad) {
        Integer id = ad.getId();
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        adService.deleteById(id);
        return ResponseUtil.ok();
    }

}
